using System;
using System.Data;
using ASPNETVN.PORTAL.Components.UI;
using ASPNETVN.PORTAL.Modules.NEWS.Components;
using ASPNETVN.PORTAL.Components.Helpers;
using System.Web.UI.WebControls;

namespace ASPNETVN.PORTAL.Modules.NEWS.Controls
{
    public partial class Item : ModulePage
    {
        private void GridDataBinding()
        {
            DataView dv;
            if (Request.QueryString["CategoryID"] != null)
            {
                // Begin build page link.
                int categoryID = Convert.ToInt32(Request.QueryString["CategoryID"]);
                PageLink pagelink = (PageLink)this.ParentControl.FindControl("MainPageLink");
                Category category = Category.Load(categoryID);
                Category parentCategory = Category.Load(category.ParentID);

                pagelink.AddLink(parentCategory.Name, "#");
                string url = ResolveClientUrl(HttpUrlBuilder.BuildUrl(this.ModuleID, 0, ModulePages.Item, "categoryid={0}", category.ID));

                pagelink.AddLink(category.Name, url);
                // End build page link.

                dv = Components.Item.SelectBy_CategoryID(Int32.Parse(Request.QueryString["categoryid"])).Tables[0].DefaultView;
            }
            else
            {
                if (Request.QueryString["OptionView"] != null)
                {
                    dv = Components.Item.GetTopItems(this.ModuleID, Request.QueryString["OptionView"]).Tables[0].DefaultView;
                }
                else
                {
                    dv = Components.Item.GetTopItems(this.ModuleID, string.Empty).Tables[0].DefaultView;
                }
            }
            gvItem.DataSource = dv;
            gvItem.DataBind();
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                this.GridDataBinding();
            }
        }

        protected void PageDropDownList_SelectedIndexChanged(Object sender, EventArgs e)
        {

            // Retrieve the pager row.
            GridViewRow pagerRow = gvItem.BottomPagerRow;

            // Retrieve the PageDropDownList DropDownList from the bottom pager row.
            DropDownList pageList = (DropDownList)pagerRow.Cells[0].FindControl("PageDropDownList");

            // Set the PageIndex property to display that page selected by the user.
            gvItem.PageIndex = pageList.SelectedIndex;
            this.GridDataBinding();

        }

        protected void gvItem_DataBound(object sender, EventArgs e)
        {
            // Retrieve the pager row.
            GridViewRow pagerRow = gvItem.BottomPagerRow;

            // Retrieve the DropDownList and Label controls from the row.
            DropDownList pageList = (DropDownList)pagerRow.Cells[0].FindControl("PageDropDownList");
            Label pageLabel = (Label)pagerRow.Cells[0].FindControl("PageLabel");

            if (pageList != null)
            {

                // Create the values for the DropDownList control based on 
                // the  total number of pages required to display the data
                // source.
                for (int i = 0; i < gvItem.PageCount; i++)
                {

                    // Create a ListItem object to represent a page.
                    int pageNumber = i + 1;
                    ListItem item = new ListItem(pageNumber.ToString());

                    // If the ListItem object matches the currently selected
                    // page, flag the ListItem object as being selected. Because
                    // the DropDownList control is recreated each time the pager
                    // row gets created, this will persist the selected item in
                    // the DropDownList control.   
                    if (i == gvItem.PageIndex)
                    {
                        item.Selected = true;
                    }

                    // Add the ListItem object to the Items collection of the 
                    // DropDownList.
                    pageList.Items.Add(item);

                }

            }

            if (pageLabel != null)
            {
                // Update the Label control with the current page information.
                pageLabel.Text = " / " + gvItem.PageCount.ToString();
            }
        }
    }
}